Ext.application({
    name: 'GS',

    requires: [
        'Ext.MessageBox',
        'Ext.data.JsonP',
        'Ext.data.Store',
        'Ext.data.proxy.JsonP',
        'Ext.TitleBar',
        'Ext.field.Email',
        'Ext.field.Password',
        'Ext.field.Select',
        'Ext.Label',
        'Ext.form.FieldSet',
        'Ext.field.Hidden',
        'Ext.field.DatePicker',
        'GS.util.Config'
    ],

    views: ['user.Register', 'user.Login', 'Main', 'post.Add', 'post.View', 'post.MyPosts', 'event.Index', 'event.Add', 'event.View', 'comment.Add', 'CategoryList', 'PostList', 'user.Profile', 'user.EditProfile'],
    models: ['user.User', 'post.Post', 'comment.Comment', 'event.Event', 'institution.Institution'],
    stores: ['UserData', 'Institutions'],
    controllers: ['User', 'Post', 'Comment', 'Event'],

    icon: {
        '57': 'resources/icons/Icon.png',
        '72': 'resources/icons/Icon~ipad.png',
        '114': 'resources/icons/Icon@2x.png',
        '144': 'resources/icons/Icon~ipad@2x.png'
    },

    isIconPrecomposed: true,

    startupImage: {
        '320x460': 'resources/startup/320x460.jpg',
        '640x920': 'resources/startup/640x920.png',
        '768x1004': 'resources/startup/768x1004.png',
        '748x1024': 'resources/startup/748x1024.png',
        '1536x2008': 'resources/startup/1536x2008.png',
        '1496x2048': 'resources/startup/1496x2048.png'
    },

    launch: function () {
        // Destroy the #appLoadingIndicator element
        Ext.fly('appLoadingIndicator').destroy();

        // Initialize the main view
        //Ext.Viewport.add(Ext.create('GS.view.Main'));

        var loginView = {
            xtype: "userlogin"
        };
        var registerView = {
            xtype: "userregister"
        };
        var mainView = {
            xtype: "main"
        };
        var editProfileView = {
            xtype: "editprofile"
        };
        var viewPostView = {
            xtype: "viewpost"
        };
        var addPostView = {
            xtype: "addpost"
        };
        var addCommentView = {
            xtype: "addcomment"
        };
        var addEventView = {
            xtype: "addevent"
        };
        var viewEventView = {
            xtype: "viewevent"
        };

        var user = Ext.create('GS.store.UserData');

        user.sync();

        user.load();

        if (user.getCount() > 0 && user.getAt(0).get('logged_in')) {
            Ext.Viewport.add([mainView, loginView, registerView, editProfileView, viewPostView, addPostView, addCommentView, addEventView, viewEventView]);
        }
        else {
            Ext.Viewport.add([loginView, registerView, mainView, editProfileView, viewPostView, addPostView, addCommentView, addEventView, viewEventView]);
        }

    },

    onUpdated: function () {
        Ext.Msg.confirm(
            "Application Update",
            "This application has just successfully been updated to the latest version. Reload now?",
            function (buttonId) {
                if (buttonId === 'yes') {
                    window.location.reload();
                }
            }
        );
    }
});
